<?php
require_once '../include/conn.php';
require_once 'header.php';
require_once 'pager.php';
$data = [];

function get_url() {
    $sys_protocal = isset($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] == '443' ? 'https://' : 'http://';
    // $php_self = $_SERVER['PHP_SELF'] ? $_SERVER['PHP_SELF'] : $_SERVER['SCRIPT_NAME'];
    // $relate_url = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : $php_self;
    return $sys_protocal.(isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : '');
}
if($clause == "addinfo"){
    if($headimg == '' || $name == '' || $account == '' || $price == '' || $url == ''){HintAndBack("请输入相关内容",1);}
    $sql = "insert into h_goods (name,account,price,url,headimg,createtime,updatetime) values ('$name','$account','$price','$url','$headimg','" . date('Y-m-d H:i:s') . "','" . date('Y-m-d H:i:s') . "')";
    $db->query($sql);
    
    $addSql = "select * from h_goods order by id desc limit 1";
    $addRes = $db->query($addSql);
    $addData = $db->fetch_array($addRes);
    
    $addId = $addData['id'];
    $exclusiveurl = get_url()."?id=".($addId);
    $sql = "update h_goods set exclusiveurl = '$exclusiveurl',updatetime='" . date('Y-m-d H:i:s') . "' where id = '$addId'";
    $db->query($sql);
        
	okinfo('?','添加成功！');
}else if($clause == "editinfo"){
        if($headimg == '' || $name == '' || $account == '' || $price == '' || $id == '' || $url == ''){HintAndBack("请输入相关内容",1);}
    
        $sql = "update h_goods set name = '$name',account = '$account',price='$price',url='$url',headimg='$headimg',updatetime='" . date('Y-m-d H:i:s') . "' where id = '$id'";
        $db->query($sql);
        
    	okinfo('?','修改成功！');
}else if($clause == "edit"){
    $sql = "select * from h_goods where id = '$id'";
    $res = $db->query($sql);
    $data = $db->fetch_array($res);
    $clause = 'editinfo';
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>商品</title>
    <link rel="stylesheet" href="index.css">
    <script src="../js/jquery.min.js"></script>
	<script src="../layer/layer.js"></script>
</head>
<body>
    <div class="form-list">
        <form action="?" method="post">
            <div class="table-title">商品</div>
            
            <div class="table-list">
                <label>卖家：</label>
                <input type="hidden" name="id" value="<?=$data['id']?>"/>
                <input type="hidden" name="clause" value="<?=$clause?:"addinfo"?>"/>
                <input type="text" name="name" maxlength="256" value="<?=$data['name']?>"/>
            </div>
            <div class="table-list">
                <label>账号：</label>
                <input type="text" name="account" maxlength="256" value="<?=$data['account']?>"/>
            </div>
            <div class="table-list">
                <label>头像：</label>
                <input type="file" accept="image/*" name="123"  id="file" onchange="upload()"/>
                <input type="hidden" id="headimg" name="headimg" value="<?=$data['headimg']?>"/>
            </div>
            <div class="table-list">
                <label>价格：</label>
                <input type="number" name="price" value="<?=$data['price']?>"/>
            </div>
            <div class="table-list">
                <label>跳转地址：</label>
                <input type="url" name="url" value="<?=$data['url']?>"/>
            </div>
            <?php
                if($clause){
                    ?>
                        <div class="table-list">
                            <label>专属地址：</label>
                            <input type="url" readonly="readonly" name="exclusiveurl" value="<?=$data['exclusiveurl']?>"/>
                        </div>
                    <?php
                }
            ?>
            <div class="btn">
                <button class="text-center">确定</button>
            </div>
        </form>
    </div>
    <div class="table-item">
        <table>
            <tr style="height: 0px;">
            </tr>
            <tr>      
              <td>id</td>
              <td>卖家</td>
              <td>账号</td>
              <td>头像</td>
              <td>价格</td>
              <td>购买地址</td>
              <td>专属地址</td>
              <td>添加时间</td>
              <td>相关操作</td>
            </tr>
            <?php
			$total_count = $db->counter('h_goods', 'id');
			$list_num = 30;
			$page = (int)$page;
			if($page_input){$page=$page_input;}
			$rowset = new Pager($total_count,$list_num,$page);
			$from_record = $rowset->_offset();
			$query = "select * from `h_goods` order by createtime desc,id desc limit $from_record, $list_num";
			$result = $db->query($query);
			while($list = $db->fetch_array($result))
			{
				$rs_list[]=$list;
			}

			$page_list = $rowset->link(GetUrl(2) . "?page=");
			
			if(count($rs_list) > 0)
			{ 
				foreach ($rs_list as $key=>$val)
				{
            ?>     
                <tr>
                <td><?php echo $val['id']; ?></td>
                <td><?php echo $val['name']; ?></td>
                <td><?php echo $val['account']; ?></td>
                <td><img width="30px" src="<?php echo $val['headimg']; ?>"/></td>
                <td><?php echo $val['price']; ?></td>
                <td><?php echo $val['url']; ?></td>
                <td><?php echo $val['exclusiveurl']; ?></td>
                <td><?php echo $val['createtime']; ?></td>
                <td>
                    <a style="cursor:pointer;" onClick="javascript:hintandturn('确定要删除吗？数据将不可恢复！','?clause=delinfo&id=<?php echo $val['id']; ?>',true);">删除</a>
                    <a style="cursor:pointer;" onClick="javascript:hintandturn('确定要修改吗？','?clause=edit&id=<?php echo $val['id']; ?>',true);">修改</a>
                </td>
                </tr>
            <?php
				}
			}	
		    ?>
        </table>
        <?php if(count($rs_list) > 0) echo "<div class=\"page\" style=\"margin-top:10px;\">{$page_list}</div>";?>
    </div>
    <?php  
    if($clause == 'delinfo'){delinfo();}

    function delinfo()
    {
    	global $db,$id;
    
    	$query = "delete from `h_goods` where id = $id";
    	$db->query($query);
    	
        echo("<script type='text/javascript'> location.href='?'; </script>");
        exit;
    }
    ?>
    <script>
        function hintandturn(str,url,torf)
        {
            if(confirm(str) && torf){
                window.location=url;
                return true;
            }else{
                return false;
            }
        }
        
        function upload(){
            var formdata=new FormData(); 
            formdata.append("file" , $("#file")[0].files[0]);
            $.ajax({
                type : 'post',
                url : 'upload.php',
                data : formdata,
                cache : false,
                processData : false,
                contentType : false, 
                success : function(res){
                    var reg = RegExp(/upload/);
                    if(res.match(reg)){
                        $("#headimg").val(res);
                    }else{
                        layer.msg(res);
                    }
                }  
            })
        }
    </script>
</body>
</html>